<?php
namespace Module\Manage;

use Model\Member as memberModel;
use Model\Model;
/**
 * 员工管理
 * @author shooke
 *
 */
class Member extends Init
{
    /**
     * 员工列表
     */
    public function index()
    {        
        $this->assign([
            'member'=>memberModel::page(['rows'=>10,'wrap'=>'dev'])->fetchAll(),//员工列表
            'pagestring'=>memberModel::pageString()//分页
        ]);
        $this->display();
    }
    /**
     * 添加员工页面
     */
    public function add()
    {
        $this->display();
    }
    /**
     * 添加员工操作
     */
    public function doAdd()
    {
        $model = new memberModel();
        $model->validate();
        $error = $model->getError();
        if($error){
            $this->error($error[0]);
        }
        $data = $model->formData();
        $data['salt']=random();
        $data['password'] = md5(md5($data['password']).$data['salt']);        
        $lastId = $model->insert($data);
        if($lastId){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    /**
     * 修改员工页面
     * @param unknown $r
     */
    public function edit($r)
    {
        $uid = $r->param('uid');
        $where = ['uid'=>$uid];
        $member = memberModel::where($where)->fetch();
        $this->assign($member);
        $this->display();
    }
    /**
     * 修改员工操作
     * @param unknown $r
     */
    public function doEdit($r)
    {
        if($r->post('username')){
            $data = ['username'=>$r->post('username')];
            $password = trim($r->post('password'));
            if(!empty($password)){
                $data['salt'] = random();
                $data['password'] = md5(md5($password).$data['salt']);
            }
            $model = new memberModel();
            $status = $model->where(['uid'=>$r->post('uid')])->data($data)->update();            
            if($status===false){
                $this->error('修改失败');
            }else{                
                $this->success('修改成功');
            }
            
        }else{
            $this->error('用户名不能为空');
        }
    }
    
}

